#define IDLE_NONE 0
#define IDLE_WAIT 1
#define IDLE_MOVE 2

#define IDLE_PRECISION 10

objectdef BotIdle inherits BotBase
{
	variable point3f Start
	variable point3f FirstPoint
	variable point3f SecondPoint
	variable point3f ThirdPoint
	variable point3f LastPoint
	variable point3f NextPoint
	variable int MaxWander = 700

	variable int ArrivedCount = 0
	variable int Next = 1
	variable string NextPosition = "ASC"

	method Initialize()
	{
		This.Start.X:Set[${Me.X}]
		This.Start.Y:Set[${Me.Y}]
	}

	method Randomize()
	{
		This.FirstPoint.X:Set[${This.AddSubtract[${Me.X},${Math.Rand[300]:Inc[25]}]}]
		This.FirstPoint.Y:Set[${This.AddSubtract[${Me.Y},${Math.Rand[300]:Inc[25]}]}]
		This.SecondPoint.X:Set[${This.AddSubtract[${Me.X},${Math.Rand[200]:Inc[25]}]}]
		This.SecondPoint.Y:Set[${This.AddSubtract[${Me.Y},${Math.Rand[100]:Inc[25]}]}]
		This.ThirdPoint.X:Set[${This.AddSubtract[${Me.X},${Math.Rand[100]:Inc[25]}]}]
		This.ThirdPoint.Y:Set[${This.AddSubtract[${Me.Y},${Math.Rand[300]:Inc[25]}]}]
		This.LastPoint.X:Set[${This.AddSubtract[${Me.X},${Math.Rand[300]:Inc[25]}]}]
		This.LastPoint.Y:Set[${This.AddSubtract[${Me.Y},${Math.Rand[200]:Inc[25]}]}]
	}

	member AddSubtract(int var1, int var2)
	{
		if ${Math.Rand[10]} < 5
		{
			return ${Math.Calc[${var1}+${var2}]}
		}
		else
		{
			return ${Math.Calc[${var1}-${var2}]}
		}
	}

	method Arrived()
	{
		if ${This.ArrivedCount} == 3
		{
			This:Randomize
			This.ArrivedCount:Set[0]
		}
		else
		{
			This.ArrivedCount:Inc
		}
	}

	method MovePulse()
	{
		if ${Math.Distance[${Me.X}, ${Me.Y}, ${This.Start.X}, ${This.Start.Y}]} > ${This.MaxWander}
		{
			This:EA["Wandering Too Far | ${This.Start.X}, ${This.Start.Y}"]
			if !${Move.HaveDestination}
			{
				Move:ToPointIdle[${This.Start.X},${This.Start.Y}]
			}
		}
		else
		{
			switch ${This.Next}
			{
				case 1
					if ${This.NextPosition.Equal[ASC]}
					{
						This.Next:Set[2]
					}
					else
					{
						This.NextPosition:Set[ASC]
						This.Next:Set[2]
					}
					This.NextPoint.X:Set[${This.FirstPoint.X}]
					This.NextPoint.Y:Set[${This.FirstPoint.Y}]
				break
				case 2
					if ${NextPosition.Equal[ASC]}
					{
						This.Next:Set[3]
					}
					else
					{
						This.Next:Set[1]
					}
					This.NextPoint.X:Set[${This.SecondPoint.X}]
					This.NextPoint.Y:Set[${This.SecondPoint.Y}]
				break
				case 3
					if ${This.NextPosition.Equal[ASC]}
					{
						This.Next:Set[4]
					}
					else
					{
						This.Next:Set[2]
					}
					This.NextPoint.X:Set[${This.ThirdPoint.X}]
					This.NextPoint.Y:Set[${This.ThirdPoint.Y}]
				break
				case 4
					if ${This.NextPosition.Equal[ASC]}
					{
						This.Next:Set[3]
						This.NextPosition:Set[DESC]
					}
					This.NextPoint.X:Set[${This.LastPoint.X}]
					This.NextPoint.Y:Set[${This.LastPoint.Y}]
				break
			}
			Move:ToPointIdle[${This.NextPoint.X},${This.NextPoint.Y}]
		}
	}

	member NeedMovement()
	{
		if !${Move.HaveDestination}
		{
			if ${Scan.FindTarget.Length} == 0 && !${Scan.GotLoot}
			{
				return TRUE
			}
		}
		return FALSE
	}

	member State()
	{
		if !${Me(exists)}
		{
			return IDLE_WAIT
		}
		
		if ${This.NeedMovement}
		{
			return IDLE_MOVE
		}

		return IDLE_NONE
	}

	method Pulse(int Priority)
	{
		This.CurrentState:Set[${This.State}]
		switch ${This.CurrentState}
		{
			case IDLE_WAIT
				break
			case IDLE_MOVE
				This:MovePulse
				break
			case IDLE_NONE
				break
		}
	}
	 
	method Inject()
	{
		This:Initialize
		This:Randomize
		Event[MOVEMENT_ARRIVED]:AttachAtom[${This.ObjectName}:Arrived]
		Event[CORE_EVENT_PULSE]:AttachAtom[${This.ObjectName}:Pulse]
	}
	 
	method Shutdown()
	{
		Event[MOVEMENT_ARRIVED]:AttachAtom[${This.ObjectName}:Arrived]
		Event[CORE_EVENT_PULSE]:DetachAtom[${This.ObjectName}:Pulse]
	}

}
